﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename
{
    public partial class Form2 : Form
    {
        private ListView listView1;
        private int customHeaderHeight = 60; // 自定义列头高度
        public Form2()
        {
            InitializeComponent();

            // 初始化 ListView
            listView1 = new ListView();
            listView1.Dock = DockStyle.Fill;
            listView1.View = View.Details;
            listView1.OwnerDraw = true; // 启用所有者绘制
            listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            // 添加列
            listView1.Columns.Add("名称", 150);
            listView1.Columns.Add("状态", 100);

            // 添加示例项
            listView1.Items.Add(new ListViewItem(new[] { "文档1", "已完成" }));
            listView1.Items.Add(new ListViewItem(new[] { "文档2", "处理中" }));
            listView1.Items.Add(new ListViewItem(new[] { "文档3", "未开始" }));

            // 绑定绘制事件
            listView1.DrawItem += ListView1_DrawItem;
            listView1.DrawSubItem += ListView1_DrawSubItem;
            listView1.DrawColumnHeader += ListView1_DrawColumnHeader;

            // 设置窗体
            this.Controls.Add(listView1);
            this.Text = "自定义绘制 ListView";
            this.Size = new Size(400, 300);
        }
        // 绘制列头
        private void ListView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            // 1. 调整列头绘制区域的高度
            Rectangle headerRect = e.Bounds;
            headerRect.Height = customHeaderHeight; // 应用自定义高度
            //// 2. 绘制列头背景（渐变效果）
            //using (LinearGradientBrush brush = new LinearGradientBrush(
            //    headerRect,
            //    Color.FromArgb(230, 230, 250),
            //    Color.FromArgb(190, 190, 230),
            //    LinearGradientMode.Vertical))
            //{
            //    e.Graphics.FillRectangle(brush, headerRect);
            //}
            //// 3. 绘制列头文本（垂直居中）
            //StringFormat format = new StringFormat
            //{
            //    Alignment = StringAlignment.Center, // 水平居中
            //    LineAlignment = StringAlignment.Center // 垂直居中
            //};
            //e.Graphics.DrawString(
            //    e.Header.Text,
            //    new Font("微软雅黑", 12, FontStyle.Bold),
            //    Brushes.Green,
            //    headerRect,
            //    format);

            // 4. 绘制列头边框
            //Rectangle rect3 = new Rectangle(headerRect.X, headerRect.Y, headerRect.Width+100, headerRect.Height+30);
            e.Graphics.DrawRectangle(Pens.Red, headerRect);

            //Rectangle rect=new Rectangle(e.Bounds.X,e.Bounds.Y,e.Bounds.Width+50,e.Bounds.Height+100);
            //// 绘制列头背景
            //using (Brush brush = new LinearGradientBrush(rect, Color.LightBlue, Color.SkyBlue, 0f))
            //{
            //    e.Graphics.FillRectangle(brush, rect);
            //}

            //// 绘制列头文本
            //e.Graphics.DrawString(e.Header.Text,
            //    new Font("微软雅黑", 9, FontStyle.Bold),
            //    Brushes.DarkBlue,
            //    rect,
            //    new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });

            //// 绘制边框
            //e.Graphics.DrawRectangle(Pens.LightGray, rect);
        }

        // 绘制项（主要用于选中状态）
        private void ListView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            //if ((e.State & ListViewItemStates.Selected) != 0)
            //{
            //    e.Graphics.FillRectangle(new SolidBrush(Color.Blue), e.Bounds);
            //}

            //// 如果项被选中，绘制选中效果
            //if ((e.State & ListViewItemStates.Selected) != 0)
            //{
            //    e.Graphics.FillRectangle(new SolidBrush(Color.LightSkyBlue), e.Bounds);
            //}
        }
        // 绘制子项
        private void ListView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            // 计算子项区域（避开自定义高度的列头）
            Rectangle subItemRect = e.Bounds;
            subItemRect.Y += customHeaderHeight - listView1.Font.Height;

            // 交替行背景色
            Color backColor = (e.Item.Index % 2 == 0) ? Color.White : Color.AliceBlue;
            e.Graphics.FillRectangle(new SolidBrush(backColor), subItemRect);

            // 绘制文本
            e.Graphics.DrawString(
                e.SubItem.Text,
                e.Item.Font,
                Brushes.Black,
                subItemRect,
                new StringFormat { LineAlignment = StringAlignment.Center }
            );
            //// 获取当前项
            //ListViewItem item = e.Item;

            //// 交替行背景色
            //Color backColor = (item.Index % 2 == 0) ? Color.White : Color.AliceBlue;

            //// 绘制背景
            //e.Graphics.FillRectangle(new SolidBrush(backColor), e.Bounds);

            //// 绘制文本
            //StringFormat format = new StringFormat { LineAlignment = StringAlignment.Center };

            //// 第二列根据状态显示不同颜色
            //Brush textBrush = Brushes.Black;
            //if (e.ColumnIndex == 1)
            //{
            //    switch (e.SubItem.Text)
            //    {
            //        case "已完成":
            //            textBrush = Brushes.Green;
            //            break;
            //        case "处理中":
            //            textBrush = Brushes.Orange;
            //            break;
            //        case "未开始":
            //            textBrush = Brushes.Red;
            //            break;
            //    }
            //    format.Alignment = StringAlignment.Center; // 第二列居中
            //}

            //e.Graphics.DrawString(e.SubItem.Text, e.Item.Font, textBrush, e.Bounds, format);

            //// 绘制单元格边框
            //e.Graphics.DrawRectangle(Pens.LightGray, e.Bounds);
        }
    }
}
